package org.zhou.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zhou.mapper.BooksMapper;
import org.zhou.pojo.*;
import org.zhou.service.BooksService;
import org.zhou.utils.ThreadLocalUtil;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

@Service
public class BooksServiceImpl implements BooksService {

    @Autowired
    private BooksMapper booksMapper;

    /**
     * 查询分类
     * @return
     */
    @Override
    public List<Type> getClassify() {
        List<Type> listType = booksMapper.getClassify();
        return listType;
    }

    @Override
    public List<Type> getByNameClassify(String name) {

        return booksMapper.getByNameClassify(name);
    }

    /**
     * 添加分类
     * @param name
     */
    @Override
    public void addClassify(String name) {
        booksMapper.addClassify(name);
    }

    /**
     * 修改分类
     * @param type
     */
    @Override
    public void updateClassify(Type type) {
        booksMapper.updateClassify(type.getId(),type.getName());
    }

    @Override
    public void deleteClassify(Integer id) {
        booksMapper.deleteClassify(id);
    }

    @Override
    public PageResult list(Integer pageNum, Integer pageSize, String bookname, Integer type, String author,String bookId) {
        //1.创建PageBean对象
        PageResult pb = new PageResult();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper

        List<Book> as = booksMapper.list(bookname,type,author,bookId);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<Book> p = (Page<Book>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void addBook(Book book) {
        booksMapper.addBook(book);
    }

    @Override
    public void updateBook(Book book) {
        booksMapper.updateBook(book);
    }

    @Override
    public void deleteBook(Integer id) {
        booksMapper.deleteBook(id);
    }

    @Override
    public PageResult borrowList(Integer pageNum, Integer pageSize, String readerId, String status, String bookId) {
        //1.创建PageBean对象
        PageResult pb = new PageResult();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper

        List<Borrow> as = booksMapper.borrowList(readerId,bookId,status);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<Borrow> p = (Page<Borrow>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public BorrowVO getBorrow(CheckBorr cb) {
        String readerId = cb.getReaderId();
        String bookId = cb.getBookId();
        Integer id = cb.getId();

        BorrowVO bo = booksMapper.getBorrow(readerId,bookId);

        LocalDate returnData = booksMapper.getBorrowDate(id);
        if(bo == null){
            return null;
        }
        bo.setReturnDate1(returnData);

        if(returnData != null){
            long val = returnData.until(LocalDate.now(), ChronoUnit.DAYS);
            if(val > 0){
                bo.setBalance1(BigDecimal.valueOf(val/10));
            }else {
                bo.setBalance1(BigDecimal.valueOf(0.0));
            }
        }else {
            bo.setBalance1(BigDecimal.valueOf(0.0));
        }

        bo.setReturnDate(LocalDate.now().plus(1, ChronoUnit.MONTHS));

        return bo;
    }

    @Override
    public void addBorrow(String readerId, String bookId) {

        booksMapper.addBorrow(readerId,bookId);
    }

    @Transactional
    @Override
    public void returned(CheckBorr cb) {

        booksMapper.returned1(cb.getId());
        booksMapper.returned2(cb.getReaderId(),cb.getBalance());

    }


}
